/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.student.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.dao.gen.bean.BizStudent;
import com.nmf.dao.gen.BizStudentMapper;

import com.nmf.site.biz.student.service.BizStudentService;

/**
 * 学生Controller
 * @author 北冥
 * @version 2019-01-02
 */
@Controller
@RequestMapping(value = "${adminPath}/student/bizStudent")
public class BizStudentController extends BaseController {

	@Autowired
	private BizStudentService bizStudentService;
	
	@ModelAttribute
	public BizStudent get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizStudent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizStudentService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizStudent();
		}
		return entity;
	}
	
	@RequiresPermissions("student:bizStudent:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizStudent bizStudent, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizStudent> page = bizStudentService.findPage(new Page<BizStudent>(request, response), bizStudent); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/student/bizStudentList";
		}
		return "biz/student/bizStudentList";
	}
	
	@RequiresPermissions("student:bizStudent:view")
	@RequestMapping(value = "form")
	public String form(BizStudent bizStudent, Model model) {
		model.addAttribute("bizStudent", bizStudent);
		return "biz/student/bizStudentForm";
	}

	@RequiresPermissions("student:bizStudent:edit")
	@RequestMapping(value = "save")
	public String save(BizStudent bizStudent, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizStudent)){
			return form(bizStudent, model);
		}
		try{
			bizStudentService.save(bizStudent);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/student/bizStudent/?repage";
		}
		addMessage(redirectAttributes, "保存学生成功");
		return "redirect:"+Global.getAdminPath()+"/student/bizStudent/?repage";
	}
	
	@RequiresPermissions("student:bizStudent:edit")
	@RequestMapping(value = "delete")
	public String delete(BizStudent bizStudent, RedirectAttributes redirectAttributes) {
		try{
			bizStudentService.delete(bizStudent);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/student/bizStudent/?repage";
		}
		addMessage(redirectAttributes, "删除学生成功");
		return "redirect:"+Global.getAdminPath()+"/student/bizStudent/?repage";
	}
	

}